import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import eslint from 'vite-plugin-eslint'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // 根据环境变量决定是否使用文件协议模式
  const isFileProtocol = process.env.FILE_PROTOCOL === 'true';
  
  return {
    plugins: [
      vue()
      // Temporarily disabled ESLint for build
      // eslint({
      //   include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
      // })
    ],
    root: process.cwd(),
    publicDir: 'public',
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        '@components': resolve(__dirname, 'src/components'),
        '@views': resolve(__dirname, 'src/views'),
        '@utils': resolve(__dirname, 'src/utils'),
        '@api': resolve(__dirname, 'src/api'),
        '@assets': resolve(__dirname, 'src/assets'),
        '@store': resolve(__dirname, 'src/store'),
        '@router': resolve(__dirname, 'src/router'),
        '@styles': resolve(__dirname, 'src/styles')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 使用新的 SASS API 避免弃用警告
          api: 'modern-compiler',
          // 确保变量和混合器正确导入
          additionalData: `@use "sass:math"; @use "sass:color"; @use "@/styles/variables.scss" as *;`
        }
      },
      // 添加CSS处理选项
      devSourcemap: false,
      postcss: {
        plugins: [
          require('autoprefixer')
        ]
      }
    },
    server: {
      port: 3000,
      open: true,
      cors: true,
      proxy: {
        '/api': {
          target: 'http://localhost:8080',
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, '/api')
        }
      }
    },
    build: {
      target: 'es2015',
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      base: isFileProtocol ? './' : '/shop/', // 文件协议使用相对路径，服务器使用子目录路径
      assetsInlineLimit: isFileProtocol ? 1024 * 1024 : 4096, // 文件协议模式下内联更多文件
      emptyOutDir: true, // 清空输出目录
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'public/index.html')
        },
        output: isFileProtocol ? {
          // 文件协议模式：生成单个文件，避免模块化问题
          format: 'umd',
          name: 'SupermarketTraceability',
          inlineDynamicImports: true,
          entryFileNames: 'js/main.js',
          assetFileNames: '[ext]/[name].[ext]',
          manualChunks: undefined,
          exports: 'none',
          // 确保不使用模块化
          globals: {
            vue: 'Vue',
            'vue-router': 'VueRouter',
            pinia: 'Pinia',
            'element-plus': 'ElementPlus'
          }
        } : {
          // 正常模式：保持模块化
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: '[ext]/[name]-[hash].[ext]',
          manualChunks: {
            vue: ['vue', 'vue-router', 'pinia'],
            element: ['element-plus', '@element-plus/icons-vue'],
            echarts: ['echarts', 'vue-echarts'],
            utils: ['axios', 'dayjs', 'lodash-es']
          }
        }
      },
      chunkSizeWarningLimit: 1000
    },
    define: {
      __VUE_OPTIONS_API__: true,
      __VUE_PROD_DEVTOOLS__: false,
      'process.env.NODE_ENV': '"production"'
    },
    esbuild: {
      drop: ['console', 'debugger']
    }
  }
})